package org.jeecg.modules.internship.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.modules.internship.entity.Internship;
import org.jeecg.modules.internship.entity.vo.QureyInternship;
import org.jeecg.modules.internship.service.InternshipService;
import org.jeecg.modules.school.service.SciSchoolService;
import org.jeecg.modules.shiro.vo.DefContants;
import org.jeecg.modules.upload.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 *
 * @author liujiamiao
 * @since 2020-10-21
 */
@RestController
@Validated
@RequestMapping("/internship/internship")
@Slf4j
@Api(tags = "基本信息接口")
public class InternshipController {
    @Autowired
    private InternshipService internshipService;

    @Autowired
    private SciSchoolService sciSchoolService;


    /**
     * 分页多条件查询
     * @param page
     * @param limit
     * @param query
     * @return
     */
    @ApiOperation(value = "分页多条件查询")
    @PostMapping("/list/{page}/{limit}")
    @PermissionData(pageComponent = "internship/Tabs")
    public Result list(@ApiParam("第几页") @PathVariable("page")Integer page,
                       @ApiParam("分页的数量") @PathVariable("limit") Integer limit,
                       @ApiParam("查询条件") @RequestBody QureyInternship query){
        IPage<Internship> pageList = internshipService.pageSelect(page,limit,query);
        return Result.ok(pageList);
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/info/{id}")
    public Result info(@ApiParam("InternshipId") @PathVariable("id") Integer id){
        Internship internship = internshipService.getById(id);
        String schoolName = sciSchoolService.querySchoolName(internship.getSchool());
        internship.setSchool(schoolName);
        return Result.ok(internship);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("internship:delete")
    public Result delete(@ApiParam("InternshipId") @PathVariable("id") Integer id){
        internshipService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @DeleteMapping("/delete/batch")
    @RequiresPermissions("internship:delete")
    public Result batchDelete(@ApiParam("id集合") @RequestBody List<Integer> ids){
        internshipService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 更新
     * @param internship
     * @return
     */
    @ApiOperation("更新")
    @PutMapping("/update")
    @RequiresPermissions("internship:modify")
    public Result update(@ApiParam("更新信息") @RequestBody @Valid Internship internship){
        String schoolCode = sciSchoolService.querySchoolCode(internship.getSchool());
        internship.setSchool(schoolCode);
        internshipService.updateById(internship);
        return Result.ok();
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    @RequiresPermissions("internship:add")
    public Result save(@ApiParam("保存") @RequestBody @Valid Internship internship){
        String schoolCode = sciSchoolService.querySchoolCode(internship.getSchool());
        internship.setSchool(schoolCode);
        internshipService.save(internship);
        return Result.ok();
    }

    @PermissionData(pageComponent = "internship/Tabs")
    @ApiOperation("导出为excel文件")
    @GetMapping("/excelExport")
    public Result excelExport(HttpServletRequest request) throws IOException {
        String localPath = internshipService.excelExport();
        //返回文件的地址给前端
        return Result.ok(localPath);
    }

    @ApiOperation("导入excel文件")
    @PostMapping("/excelImport")
    public Result excelImport(@RequestParam(value = "file") MultipartFile excel) throws Exception {
        InputStream inputStream = excel.getInputStream();
        internshipService.excelImport(inputStream);
        return  Result.ok();
    }

    @ApiOperation("保存实习生的相头")
    @PostMapping("/upload")
    public Result uploadImage(@RequestParam(value = "image") MultipartFile uploadImage) throws IOException {
        String localPath = FileUploadUtils.upload(uploadImage,"images");
        return Result.ok(localPath);
    }

    @ApiOperation("导出word")
    @GetMapping("/downloadword/{id}")
    public Result downloadword(@PathVariable("id")Integer id) throws IOException, InvalidFormatException {
        String localPath = internshipService.downLoadWord(id);
        return Result.ok(localPath);
    }

}

